package com.shuqi.activity.adapter;

import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.activity.MyFavoritActivity;
import com.shuqi.common.utils.SharedPreferenceUtil;
import com.shuqi.common.utils.SqImageManager;
import com.shuqi.controller.R;
import com.shuqi.model.bean.MyFavoritBean.MyFavorityItemBean;

/**
 * 
 * 我的收藏Adapter
 * 
 * @author li.li
 * @date 2014-06-11
 */
public class MyFavoritAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private List<MyFavorityItemBean> list;
    private Handler handler;
    private int delPosition;// 当前删除处理pisition
    private Context context;

    public MyFavoritAdapter(Context context, Handler handler) {
        this.inflater = LayoutInflater.from(context);
        this.inflater = LayoutInflater.from(context);
        this.handler = handler;
        this.context = context;
    }

    public void setData(List<MyFavorityItemBean> list) {
        this.list = list;
    }

    @Override
    public int getCount() {

        return list == null ? 0 : list.size();

    }

    @Override
    public Object getItem(int position) {
        if (list == null)
            return null;
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final MyFavorityItemBean myFavorityItemBean = list.get(position);
        View view = null;
        Holder holder = null;
        if (convertView != null) {
            view = convertView;
            holder = (Holder) view.getTag();
        } else {
            view = inflater.inflate(R.layout.item_my_favorit, parent, false);

            holder = (Holder) view.getTag();
            if (holder == null) {
                holder = new Holder();
                holder.myFavoritTitleImage =
                        (ImageView) view.findViewById(R.id.my_favorit_title_image);
                holder.myFavoritBooknameText =
                        (TextView) view.findViewById(R.id.my_favorit_bookname_text);
                holder.myFavoritAuthorText =
                        (TextView) view.findViewById(R.id.my_favorit_author_text);
                holder.myFavoritDateText = (TextView) view.findViewById(R.id.my_favorit_date_text);
                holder.myFavoritRemoveButton =
                        (Button) view.findViewById(R.id.my_favorit_remove_button);

                view.setTag(holder);
            }
        }

        // 绑定事件
        holder.myFavoritRemoveButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                delFavoritBook(myFavorityItemBean, position);
            }
        });

        if (myFavorityItemBean != null) {
            holder.myFavoritBooknameText.setText(myFavorityItemBean.getBookName());
            holder.myFavoritDateText.setText(myFavorityItemBean.colTime());
            holder.myFavoritAuthorText.setText(myFavorityItemBean.getAuthor());
            SqImageManager.getImageManager().displayImage(myFavorityItemBean.getImgUrl(),
                    holder.myFavoritTitleImage, SqImageManager.IMG_COVER);

            if (isCanEdit())
                holder.myFavoritRemoveButton.setVisibility(View.VISIBLE);
            else
                holder.myFavoritRemoveButton.setVisibility(View.GONE);


        }
        return view;
    }

    private static class Holder {
        public ImageView myFavoritTitleImage;
        public TextView myFavoritBooknameText;
        public TextView myFavoritAuthorText;
        public TextView myFavoritDateText; // 时间
        public Button myFavoritRemoveButton;
    }

    public void setCanEdit(boolean canEdit) {
        SharedPreferenceUtil.setFavoritIsEdit(context, canEdit);
    }

    public boolean isCanEdit() {
        boolean canEdit = SharedPreferenceUtil.isMyFavoritEdit(context);
        return canEdit;
    }

    /**
     * 删除并刷新
     * 
     * @param position
     */
    public void remove() {
        list.remove(delPosition);
        notifyDataSetChanged();
    }

    public List<MyFavorityItemBean> getList() {
        return list;
    }

    public void delFavoritBook(MyFavorityItemBean myFavoritItemBean, int position) {
        delPosition = position;// 当前处理positon

        Message msg = Message.obtain();
        msg.what = MyFavoritActivity.my_favorit_remove;
        Bundle bundle = new Bundle();
        bundle.putString("bookId", myFavoritItemBean.getBookId());
        msg.setData(bundle);
        handler.sendMessage(msg);
    }
}
